<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class album_uqcms extends control
{
    function __construct()
    {
        $this->spid = @$_SESSION['shop']['spid'];
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    public function index()
    {
        $size = $this->db->get_one("select SUM(size) as sum from " . table('album') . " where shop_id = " . $this->spid);
        $space_limit = cfg('shop_album_space') ? cfg('shop_album_space') : 32212254720;
        $space_size = $size['sum'];
        $space['limit'] = format_bytes($space_limit);
        $space['size'] = format_bytes($space_size);
        $space['percent'] = $space_size / $space_limit;
        $this->assign('space', $space);
        $this->display();
    }
    public function upload()
    {
        $path = isset($_GET['path']) ? $_GET['path'] : 'goods';
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '0';
        $result = $this->module('album')->upload($path, $cid);
        echo json_encode($result);
    }
    public function upload_temp()
    {
        $result = upload::store('temp/images');
        echo json_encode($result);
    }
    public function sp_logo()
    {
        $row = $this->db->get_one("select id,logo from " . table('shop') . " where id = " . $this->spid);
        if ($row['id']) {
            $this->module('album')->del($row['logo']);
            $result = $this->module('album')->upload('shop');
            $query = $this->db->update(table('shop'), array('logo' => $result['name']), 'id =' . $this->spid);
            if ($query) {
                echo json_encode($result);
            } else {
                error_json('更新错误');
            }
        } else {
            error_json('请重新登陆');
        }
    }
    public function del()
    {
        $id = P('id', '提交错误');
        $row = $this->db->get_one("select id,shop_id,url from " . table('album') . " where shop_id = " . $this->spid . " and id = " . $id);
        if ($row['id']) {
            $status = $this->module('album')->del($row['url']);
            if ($status) {
                right_json();
            } else {
                error_json('删除失败');
            }
        } else {
            error_json('删除失败');
        }
    }
    public function ajax_del()
    {
        $ck_id = !empty($_POST['ck_id']) ? $_POST['ck_id'] : exit('ID ERROR');
        $arr = explode(',', $ck_id);
        $arr_num = count($arr);
        if ($arr_num > 0) {
            $data = $this->db->get_all("select id,shop_id,url from " . table('album') . " where id in(" . $ck_id . ") and shop_id = " . $this->spid);
            if ($data) {
                foreach ($data as $k => $v) {
                    $status = $this->module('album')->del($v['url']);
                }
                right_json();
            } else {
                error_json('无图片删除');
            }
        } else {
            error_json('删除失败');
        }
    }
    public function cut_img()
    {
        if ($_POST) {
            $img_url = isset($_POST['img']) ? $_POST['img'] : error_json('图片不能为空');
            $x1 = $_POST['x1'];
            $y1 = $_POST['y1'];
            $w = $_POST['w'];
            $h = $_POST['h'];
            $row = $this->db->get_one("select uid,avatar from " . table('member') . " where uid = " . $this->uid);
            if ($row['uid']) {
                $this->module('album')->del($row['avatar']);
                $result_url = images::crop($img_url, $x1, $y1, $w, $h, 'temp/images');
                $result_arr = $this->module('album')->upload_move($result_url, 'avatar');
                if ($result_arr['name']) {
                    $query = $this->db->update(table('member'), array('avatar' => $result_arr['name']), 'uid = ' . $this->uid);
                    if ($query) {
                        $arr['error'] = '0';
                        $arr['msg'] = $result_arr['name'];
                        $_SESSION['user']['avatar'] = $result_arr['name'];
                        echo json_encode($arr);
                    } else {
                        error_json('保存错误');
                    }
                } else {
                    error_json('剪切错误');
                }
            } else {
                error_json('用户不存在，请重新登陆');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function type()
    {
        $result = $this->db->get_all("select * from " . table('album_type') . " where shop_id = " . $this->spid . " order by px desc");
        if ($result) {
            foreach ($result as $k => $v) {
                $data[] = $v;
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function type_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('album_type') . " where id = " . $id . " and shop_id = " . $this->spid);
            if ($row['id']) {
                $this->assign('row', $row);
            } else {
                error_404('提交错误');
            }
        }
        $this->display();
    }
    public function type_edit_save()
    {
        if ($_POST) {
            $setarr['pid'] = P('pid', '', '0');
            $setarr['name'] = P('name', '分类名称不能为空');
            $setarr['px'] = P('px', '', '0');
            $setarr['addtime'] = time();
            $id = P('id', false);
            if ($id) {
                $query = $this->db->update(table('album_type'), $setarr, 'shop_id = ' . $this->spid . ' and id = ' . $id);
            } else {
                $setarr['shop_id'] = $this->spid;
                $query = $this->db->add(table('album_type'), $setarr);
                $id = $this->db->insert_id();
            }
            if ($query) {
                $arr['error'] = '0';
                $arr['id'] = $id;
                echo json_encode($arr);
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function type_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : error_json('提交错误');
        if ($id == '0') {
            error_json('已经是顶级目录，不能删除');
        }
        $album_num = $this->db->get_one("select count(id) as total from " . table('album') . " where shop_id = " . $this->spid . " and utype = " . $id);
        if ($album_num['total'] == '0') {
            $type_num = $this->db->get_one("select count(id) as total from " . table("album_type") . " where shop_id = " . $this->spid . " and pid = " . $id);
            if ($type_num['total'] == '0') {
                $query = $this->db->delete(table('album_type'), 'id = ' . $id . ' and shop_id = ' . $this->spid);
                if ($query) {
                    right_json();
                } else {
                    error_json('删除失败');
                }
            } else {
                error_json('有子目录，无非删除');
            }
        } else {
            error_json('相册中有图片,清空目录图片再删除');
        }
    }
    public function update_img_type()
    {
        if ($_POST) {
            $type = isset($_POST['type']) ? $_POST['type'] : error_json('没有选择目录');
            $ck_id = isset($_POST['ck_id']) ? $_POST['ck_id'] : error_json('选择目录错误');
            $setarr['utype'] = $type;
            $setarr['addtime'] = time();
            $query = $this->db->update(table('album'), $setarr, 'shop_id = ' . $this->spid . ' and id in (' . $ck_id . ')');
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function ajax_type()
    {
        $pid = isset($_GET['pid']) ? $_GET['pid'] : '0';
        $data = $this->db->get_all("select id,pid,shop_id,name from " . table('album_type') . " where pid = " . $pid . " and shop_id = " . $this->spid . " order by id,px desc");
        if ($data) {
            foreach ($data as $k => $v) {
                $arr[] = $v;
            }
            $result['error'] = '0';
            $result['list'] = $arr;
            echo json_encode($result);
        } else {
            error_json('分类为空');
        }
    }
    public function ajax_list()
    {
        $type = isset($_GET['type']) ? $_GET['type'] : '0';
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '21';
        $img_host = img_host();
        $img_size = img_size('120x120');
        $wsql = ' and utype = ' . $type . ' ';
        $data = $this->db->get_all("select * from " . table('album') . " where shop_id = " . $this->spid . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($data) {
            foreach ($data as $k => $v) {
                $v['images'] = $img_host . $v['url'] . $img_size;
                $html[] = $v;
            }
            $html_arr['html'] = $html;
            $total = $this->db->get_one("select count(id) as total from " . table('album') . " where shop_id = " . $this->spid . $wsql);
            if ($total['total'] > $page_size) {
                $html_arr['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            } else {
                $html_arr['page'] = '';
            }
        } else {
            $html_arr['html'] = array();
            $html_arr['page'] = '';
        }
        $html_arr['type'] = $type;
        echo json_encode($html_arr);
    }
}